﻿using System.Collections.Generic;
using CnSharp.Database.MsSql;
using CnSharp.Database.Objects;
using CnSharp.SSMS.AddIn.Util;

namespace CnSharp.SSMS.AddIn.Framework.Cache
{
    public class DbObjectLoader
    {
        public static List<Table> GetTables(DbObjectContext context)
        {
            var db = ObjectCache.Instance.Get(context.Server, context.Database);
            return db != null ? db.Tables : null;
        }

        public static Table GetTable(DbObjectContext context)
        {
            var table = ObjectCache.Instance.GetTable(context) ?? GetTableRealTime(context);
            return table;
        }

        public static Table GetTableRealTime(DbObjectContext context)
        {
            var db = new MsSql(context.ConnectionString);
            return db.GetTable(context.Name);
        }

        public static View GetView(DbObjectContext context)
        {
            var view = ObjectCache.Instance.GetView(context) ?? GetViewRealTime(context);
            return view;
        }

        public static View GetViewRealTime(DbObjectContext context)
        {
            var db = new MsSql(context.ConnectionString);
            return db.GetView(context.Name);
        }

        //public static ProgramScript GetProcedure(DbObjectContext context)
        //{
        //    var sp = ObjectCache.Instance.GetStoredProcedure(context) ?? GetProcedureRealTime(context);
        //    return sp;
        //}

        //public static ProgramScript GetProcedureRealTime(DbObjectContext context)
        //{
        //    var db = new MsSql(context.ConnectionString);
        //    var parms = db.GetSpParameters(context.Name, true);
        //    var script = new ProgramScript {Name = context.Name, Parameters = parms};
        //    return script;
        //}
    }
}
